import { envConstants } from '@app/constants';
import { logger } from '@app/plugins/logger';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

import { setupSwagger } from './swagger';

const setupApp = (app: INestApplication) => {
  const config = app.get<ConfigService>(ConfigService);
  app.useLogger(logger);

  if (import.meta.env.PROD) {
    async function bootstrap() {
      await app.listen(config.get<number>(envConstants.port, 3000));
      console.log(`The application is running on ${app.getUrl()}`);
    }
    bootstrap();
  }
  app.setGlobalPrefix(config.get<string>(envConstants.endPointPrefix, ''));
  setupSwagger(app, config);

  app.useGlobalPipes(new ValidationPipe());

  app.enableCors();
};
export default setupApp;
